<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">



  
  
    
      
    
    
      
    
  <script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
  <link href="//cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-flash.min.css" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
    
  
  <link href="//cdn.jsdelivr.net/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet" type="text/css">







  

<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico?v=">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.ico?v=">


  <link rel="mask-icon" href="/images/logo.svg?v=" color="#222">





  <meta name="keywords" content="docker,">





  <link rel="alternate" href="/atom.xml" title="WhiteWater" type="application/atom+xml">






<meta name="description" content="使用docker部署springboot项目 前言将Docker通过插件的方式集成到SpringBoot中，然后通过Maven命令根据项目中的Dockerfile自动生成Docker镜像，同时将镜像推送到远程docker仓库中，然后在服务器通过pull镜像启动Docker来启动SpringBoot项目，并访问接口。">
<meta name="keywords" content="docker">
<meta property="og:type" content="article">
<meta property="og:title" content="docker入门(三) docker整合springboot项目">
<meta property="og:url" content="http://yuanww.cn/2020/02/27/docker入门-三/index.html">
<meta property="og:site_name" content="WhiteWater">
<meta property="og:description" content="使用docker部署springboot项目 前言将Docker通过插件的方式集成到SpringBoot中，然后通过Maven命令根据项目中的Dockerfile自动生成Docker镜像，同时将镜像推送到远程docker仓库中，然后在服务器通过pull镜像启动Docker来启动SpringBoot项目，并访问接口。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://yuanww.cn/2020/02/27/docker入门-三/docker.jpg">
<meta property="og:image" content="http://yuanww.cn/2020/02/27/docker入门-三/1583661546032.png">
<meta property="og:image" content="http://yuanww.cn/2020/02/27/docker入门-三/1583661139999.png">
<meta property="og:image" content="http://yuanww.cn/2020/02/27/docker入门-三/1583661422497.png">
<meta property="og:image" content="http://yuanww.cn/2020/02/27/docker入门-三/1583661451294.png">
<meta property="og:image" content="http://yuanww.cn/2020/02/27/docker入门-三/1583663474460.png">
<meta property="og:image" content="http://yuanww.cn/2020/02/27/docker入门-三/1583662850195.png">
<meta property="og:image" content="http://yuanww.cn/2020/02/27/docker入门-三/1583665034066.png">
<meta property="og:image" content="http://yuanww.cn/2020/02/27/docker入门-三/1583664984673.png">
<meta property="og:image" content="http://yuanww.cn/2020/02/27/docker入门-三/6944619-06b84e9a5c3beb98.png">
<meta property="og:image" content="http://yuanww.cn/2020/02/27/docker入门-三/1583661879303.png">
<meta property="og:image" content="http://yuanww.cn/2020/02/27/docker入门-三/1583673049110.png">
<meta property="og:image" content="http://yuanww.cn/2020/02/27/docker入门-三/1583673656207.png">
<meta property="og:updated_time" content="2020-03-08T14:08:07.630Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="docker入门(三) docker整合springboot项目">
<meta name="twitter:description" content="使用docker部署springboot项目 前言将Docker通过插件的方式集成到SpringBoot中，然后通过Maven命令根据项目中的Dockerfile自动生成Docker镜像，同时将镜像推送到远程docker仓库中，然后在服务器通过pull镜像启动Docker来启动SpringBoot项目，并访问接口。">
<meta name="twitter:image" content="http://yuanww.cn/2020/02/27/docker入门-三/docker.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yuanww.cn/2020/02/27/docker入门-三/">





  <title>docker入门(三) docker整合springboot项目 | WhiteWater</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?e46442d1930481b98d53762512c55bc1";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">WhiteWater</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">Thirst most</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yuanww.cn/2020/02/27/docker入门-三/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="ctrl">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/bbb.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="WhiteWater">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">docker入门(三) docker整合springboot项目</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-02-27T23:28:08+08:00">
                2020-02-27
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/docker/" itemprop="url" rel="index">
                    <span itemprop="name">docker</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2020/02/27/docker入门-三/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2020/02/27/docker入门-三/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/2020/02/27/docker入门-三/" class="leancloud_visitors" data-flag-title="docker入门(三) docker整合springboot项目">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.2k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  5
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="使用docker部署springboot项目"><a href="#使用docker部署springboot项目" class="headerlink" title="使用docker部署springboot项目"></a>使用docker部署springboot项目</h1><p><img src="/2020/02/27/docker入门-三/docker.jpg" alt></p>
<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>将Docker通过插件的方式集成到SpringBoot中，然后通过Maven命令根据项目中的Dockerfile自动生成Docker镜像，同时将镜像推送到远程docker仓库中，然后在服务器通过pull镜像启动Docker来启动SpringBoot项目，并访问接口。</p>
<a id="more"></a>
<h2 id="环境搭建"><a href="#环境搭建" class="headerlink" title="环境搭建"></a>环境搭建</h2><h3 id="准备环境"><a href="#准备环境" class="headerlink" title="准备环境"></a>准备环境</h3><blockquote>
<p>jdk</p>
<p>maven</p>
<p>docker</p>
</blockquote>
<h3 id="springboot项目"><a href="#springboot项目" class="headerlink" title="springboot项目"></a>springboot项目</h3><h4 id="pom-xml-增加docker依赖"><a href="#pom-xml-增加docker依赖" class="headerlink" title="pom.xml 增加docker依赖"></a>pom.xml 增加docker依赖</h4><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">&lt;properties&gt;</span><br><span class="line">	&lt;!-- docker service --&gt;</span><br><span class="line">	&lt;docker.registryUrl&gt;http:<span class="comment">//101.xxx.xxx.167:2375 &lt;/docker.registryUrl&gt;					</span></span><br><span class="line">        &lt;docker.imageName&gt;101.xxx.xxx.167/library/$&#123;project.artifactId&#125;:1.0&lt;/docker.imageName&gt;</span><br><span class="line">&lt;/properties&gt;</span><br><span class="line"></span><br><span class="line">&lt;build&gt;</span><br><span class="line">	&lt;plugins&gt;</span><br><span class="line">        	&lt;plugin&gt;</span><br><span class="line">			&lt;groupId&gt;com.spotify&lt;/groupId&gt;</span><br><span class="line">			&lt;artifactId&gt;docker-maven-plugin&lt;/artifactId&gt;</span><br><span class="line">			&lt;version&gt;1.1.0&lt;/version&gt;</span><br><span class="line">			&lt;configuration&gt;</span><br><span class="line">				&lt;!-- 连接远程仓库 --&gt;</span><br><span class="line">				&lt;serverId&gt;docker-harbor&lt;/serverId&gt;</span><br><span class="line">				&lt;!-- 远程仓库地址 --&gt;</span><br><span class="line">				&lt;registryUrl&gt;$&#123;docker.registryUrl&#125;&lt;/registryUrl&gt;</span><br><span class="line">				&lt;dockerHost&gt;$&#123;docker.registryUrl&#125;&lt;/dockerHost&gt;</span><br><span class="line">				&lt;!-- 这里是最终生成的docker镜像名称 注意imageName一定要是符合正则[a-z0-<span class="number">9</span>-_.]的，否则构建不会成功 --&gt;</span><br><span class="line">				&lt;imageName&gt;$&#123;docker.imageName&#125;&lt;/imageName&gt;</span><br><span class="line">				&lt;dockerDirectory&gt;$&#123;project.basedir&#125;/bin/docker&lt;/dockerDirectory&gt;</span><br><span class="line">				&lt;!-- resources的内容会复制  dockerDirectory (也就是构建上下文)中 --&gt;</span><br><span class="line">       			        &lt;resources&gt;</span><br><span class="line">					&lt;resource&gt;</span><br><span class="line">						&lt;targetPath&gt;/&lt;/targetPath&gt;</span><br><span class="line">						&lt;directory&gt;$&#123;project.build.directory&#125;&lt;/directory&gt;</span><br><span class="line">						&lt;include&gt;web.war&lt;/include&gt;</span><br><span class="line">					&lt;/resource&gt;</span><br><span class="line">				&lt;/resources&gt;</span><br><span class="line">				&lt;!-- 覆盖镜像 --&gt;</span><br><span class="line">				&lt;forceTags&gt;true&lt;/forceTags&gt;</span><br><span class="line">				&lt;!--  推送镜像 --&gt;</span><br><span class="line">				&lt;pushImage&gt;true&lt;/pushImage&gt;</span><br><span class="line">			&lt;/configuration&gt;</span><br><span class="line">			&lt;!-- 项目<span class="keyword">package</span>时会执行docker build 命令 --&gt;</span><br><span class="line">			&lt;executions&gt;</span><br><span class="line">				&lt;execution&gt;</span><br><span class="line">					&lt;id&gt;build-image&lt;/id&gt;</span><br><span class="line">					&lt;phase&gt;package&lt;/phase&gt;</span><br><span class="line">					&lt;goals&gt;</span><br><span class="line">						&lt;goal&gt;build&lt;/goal&gt;</span><br><span class="line">					&lt;/goals&gt;</span><br><span class="line">				&lt;/execution&gt;</span><br><span class="line">			&lt;/executions&gt;</span><br><span class="line">		&lt;/plugin&gt;</span><br><span class="line">	&lt;/plugins&gt;</span><br><span class="line">&lt;/build&gt;</span><br></pre></td></tr></table></figure>
<h4 id="新增-Dockerfile-文件"><a href="#新增-Dockerfile-文件" class="headerlink" title="新增 Dockerfile 文件"></a>新增 Dockerfile 文件</h4><p>在项目的根目录下新增bin/docker/Dockerfile文件</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">FROM openjdk:<span class="number">8</span>-jdk-alpine</span><br><span class="line">MAINTAINER <span class="number">123456</span><span class="meta">@qq</span>.com</span><br><span class="line">ENV TZ <span class="string">"Asia/Shanghai"</span></span><br><span class="line">ENV LANG C.UTF-<span class="number">8</span></span><br><span class="line">VOLUME /tmp</span><br><span class="line"></span><br><span class="line">WORKDIR /app</span><br><span class="line"></span><br><span class="line">#添加打包好的war包</span><br><span class="line">ADD web.war .</span><br><span class="line">RUN jar -xvf web.war</span><br><span class="line"></span><br><span class="line">WORKDIR /app/WEB-INF</span><br><span class="line">ENV JAVA_OPTS <span class="string">"-Xms256m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m"</span></span><br><span class="line">ENV JAVA_OPTS <span class="string">"$JAVA_OPTS -Dspring.profiles.active=prod"</span></span><br><span class="line"></span><br><span class="line"># 添加项目配置文件    </span><br><span class="line">ADD application-prod.yml ./classes/config</span><br><span class="line"></span><br><span class="line">ENTRYPOINT java -cp /app $JAVA_OPTS org.springframework.boot.loader.WarLauncher</span><br><span class="line"></span><br><span class="line"># 项目映射端口</span><br><span class="line">EXPOSE <span class="number">8086</span></span><br></pre></td></tr></table></figure>
<h2 id="docker远程仓库"><a href="#docker远程仓库" class="headerlink" title="docker远程仓库"></a>docker远程仓库</h2><h4 id="docker私服仓库harbor"><a href="#docker私服仓库harbor" class="headerlink" title="docker私服仓库harbor"></a>docker私服仓库harbor</h4><p>Harbor是构建企业级私有docker镜像的仓库的开源解决方案，它是Docker Registry的更高级封装，从而获得了更加广泛的应用，这些新的企业级特性包括：管理用户界面，基于角色的访问控制 ，AD/LDAP集成以及审计日志等，足以满足基本企业需求。</p>
<p>hardor的安装可自行百度 本文已经安装好hardor </p>
<p><img src="/2020/02/27/docker入门-三/1583661546032.png" alt="1583661546032"></p>
<h4 id="maven-设置-setting"><a href="#maven-设置-setting" class="headerlink" title="maven 设置 setting"></a>maven 设置 setting</h4><p>在maven的setting文件中找到server节点加入Harbor的服务信息</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;server&gt;</span><br><span class="line">      &lt;!--maven的pom中可以根据这个id找到这个server节点的配置--&gt;  </span><br><span class="line">      &lt;id&gt;docker-harbor&lt;/id&gt;</span><br><span class="line">      &lt;username&gt;admin&lt;/username&gt;</span><br><span class="line">      &lt;password&gt;password&lt;/password&gt;</span><br><span class="line">	  &lt;configuration&gt;</span><br><span class="line">       &lt;email&gt;somebody@mail.com&lt;/email&gt;</span><br><span class="line">    &lt;/configuration&gt;</span><br><span class="line">&lt;/server&gt;</span><br></pre></td></tr></table></figure>
<h2 id="打包推送镜像"><a href="#打包推送镜像" class="headerlink" title="打包推送镜像"></a>打包推送镜像</h2><h3 id="执行package命令"><a href="#执行package命令" class="headerlink" title="执行package命令"></a>执行package命令</h3><p><img src="/2020/02/27/docker入门-三/1583661139999.png" alt="1583661139999"></p>
<h3 id="bulid镜像"><a href="#bulid镜像" class="headerlink" title="bulid镜像"></a>bulid镜像</h3><p><img src="/2020/02/27/docker入门-三/1583661422497.png" alt="1583661422497"></p>
<h3 id="push镜像"><a href="#push镜像" class="headerlink" title="push镜像"></a>push镜像</h3><p><img src="/2020/02/27/docker入门-三/1583661451294.png" alt="1583661451294"></p>
<h3 id="在docker服务器端查看镜像"><a href="#在docker服务器端查看镜像" class="headerlink" title="在docker服务器端查看镜像"></a>在docker服务器端查看镜像</h3><p><img src="/2020/02/27/docker入门-三/1583663474460.png" alt="1583663474460"></p>
<h3 id="查看镜像仓库"><a href="#查看镜像仓库" class="headerlink" title="查看镜像仓库"></a>查看镜像仓库</h3><p><img src="/2020/02/27/docker入门-三/1583662850195.png" alt="1583662850195"></p>
<h3 id="启动镜像"><a href="#启动镜像" class="headerlink" title="启动镜像"></a>启动镜像</h3><p><img src="/2020/02/27/docker入门-三/1583665034066.png" alt="1583665034066"></p>
<h3 id="访问项目"><a href="#访问项目" class="headerlink" title="访问项目"></a>访问项目</h3><p><img src="/2020/02/27/docker入门-三/1583664984673.png" alt="1583664984673"></p>
<h2 id="整合jenkins实现自动部署"><a href="#整合jenkins实现自动部署" class="headerlink" title="整合jenkins实现自动部署"></a>整合jenkins实现自动部署</h2><h3 id="自动化部署"><a href="#自动化部署" class="headerlink" title="自动化部署"></a>自动化部署</h3><p>随着DevOps理念和敏捷理念的发展，我们希望通过自动化技术，加快项目的迭代。尤其是当使用微服务方案后，面临在大量的项目构建和部署工作，借助于jenkins的持续集成，可以快速把应用打包成docker镜像，实现自动部署。</p>
<p><img src="/2020/02/27/docker入门-三/6944619-06b84e9a5c3beb98.png" alt></p>
<p>如图演示了以下的场景：</p>
<ul>
<li>开发者向自己的gitlab网站提交了代码</li>
<li>jenkins通过定时任务检测到了代码有变成，执行自动化构建过程</li>
<li>jenkins在自动化构建脚本中调用docker命令将构建好的镜像push 到harbor私有镜像仓库</li>
<li>同时，jenkins也可以直接执行shell启动构建好的容器</li>
<li>构建失败或者成功，可以及时将结果推送给相关人员，比如测试人员，安排测试</li>
<li>服务端可以手动通过docker命令，从镜像仓库中心拉取镜像，进行手动部署</li>
</ul>
<h3 id="jenkins构建项目"><a href="#jenkins构建项目" class="headerlink" title="jenkins构建项目"></a>jenkins构建项目</h3><p>代码托管平台使用的是gitlab 所以要在jenkins上安装gitlab插件</p>
<p><img src="/2020/02/27/docker入门-三/1583661879303.png" alt></p>
<p>新建一个maven项目，添加gitlab代码地址</p>
<p><img src="/2020/02/27/docker入门-三/1583673049110.png" alt="1583673049110"></p>
<p>项目构建成功 执行shell 启动镜像</p>
<p><img src="/2020/02/27/docker入门-三/1583673656207.png" alt="1583673656207"></p>
<h2 id="结语"><a href="#结语" class="headerlink" title="结语"></a>结语</h2><p><strong>基于Docker+Jenkins+Gitlab搭建持续集成环境的教程到此结束了，说实话第一次接触这些确实不好弄，自己研究了两个礼拜才把整套流程完整的走了一遍，记录的也很粗糙，有些细节写的不是很清楚，中间踩了很多很多坑。很高兴自己能坚持下来，也学到了不少东西，继续加油吧！</strong></p>

      
    </div>
    
    
    
    <div>
      
        
<div class="my_post_copyright">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <p><span>本文标题:</span><a href="/2020/02/27/docker入门-三/">docker入门(三) docker整合springboot项目</a></p>
  <p><span>文章作者:</span><a href="/" title="访问 ctrl 的个人博客">ctrl</a></p>
  <p><span>发布时间:</span>2020年02月27日 - 23:02</p>
  <p><span>最后更新:</span>2020年03月08日 - 22:03</p>
  <p><span>原始链接:</span><a href="/2020/02/27/docker入门-三/" title="docker入门(三) docker整合springboot项目">http://yuanww.cn/2020/02/27/docker入门-三/</a>
    <span class="copy-path" title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="http://yuanww.cn/2020/02/27/docker入门-三/" aria-label="复制成功！"></i></span>
  </p>
  <p><span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>
</div>
<script>
    var clipboard = new Clipboard('.fa-clipboard');
    $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({
          title: "",
          text: '复制成功',
          icon: "success",
          showConfirmButton: true
          });
    });
    });
</script>


      
    </div>

    

    <div>
      
        <div>
    
        <br>
        <div style="text-align:center;color: #ccc;font-size:30px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      
    </div>

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/docker/" rel="tag"><i class="fa fa-tag"></i> docker</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/02/24/docker入门-二/" rel="next" title="docker入门(二) docker命令使用">
                <i class="fa fa-chevron-left"></i> docker入门(二) docker命令使用
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        
  <div class="bdsharebuttonbox">
    <a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
    <a href="#" class="bds_douban" data-cmd="douban" title="分享到豆瓣网"></a>
    <a href="#" class="bds_sqq" data-cmd="sqq" title="分享到QQ好友"></a>
    <a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间"></a>
    <a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信"></a>
    <a href="#" class="bds_tieba" data-cmd="tieba" title="分享到百度贴吧"></a>
    <a href="#" class="bds_twi" data-cmd="twi" title="分享到Twitter"></a>
    <a href="#" class="bds_fbook" data-cmd="fbook" title="分享到Facebook"></a>
    <a href="#" class="bds_more" data-cmd="more"></a>
    <a class="bds_count" data-cmd="count"></a>
  </div>
  <script>
    window._bd_share_config = {
      "common": {
        "bdText": "",
        "bdMini": "2",
        "bdMiniList": false,
        "bdPic": ""
      },
      "share": {
        "bdSize": "16",
        "bdStyle": "0"
      },
      "image": {
        "viewList": ["tsina", "douban", "sqq", "qzone", "weixin", "twi", "fbook"],
        "viewText": "分享到：",
        "viewSize": "16"
      }
    }
  </script>

<script>
  with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
</script>

      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/bbb.gif" alt="ctrl">
            
              <p class="site-author-name" itemprop="name">ctrl</p>
              <p class="site-description motion-element" itemprop="description">毕业时长两年半的CV工程师</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">20</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">13</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/yuan2658" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://juejin.im" title="掘金" target="_blank">掘金</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.zhihu.com" title="知乎" target="_blank">知乎</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.spring4all.com" title="Spring For All" target="_blank">Spring For All</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.v2ex.com" title="印记中文" target="_blank">印记中文</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#使用docker部署springboot项目"><span class="nav-number">1.</span> <span class="nav-text">使用docker部署springboot项目</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#前言"><span class="nav-number">1.1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#环境搭建"><span class="nav-number">1.2.</span> <span class="nav-text">环境搭建</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#准备环境"><span class="nav-number">1.2.1.</span> <span class="nav-text">准备环境</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#springboot项目"><span class="nav-number">1.2.2.</span> <span class="nav-text">springboot项目</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#pom-xml-增加docker依赖"><span class="nav-number">1.2.2.1.</span> <span class="nav-text">pom.xml 增加docker依赖</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#新增-Dockerfile-文件"><span class="nav-number">1.2.2.2.</span> <span class="nav-text">新增 Dockerfile 文件</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#docker远程仓库"><span class="nav-number">1.3.</span> <span class="nav-text">docker远程仓库</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#docker私服仓库harbor"><span class="nav-number">1.3.0.1.</span> <span class="nav-text">docker私服仓库harbor</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#maven-设置-setting"><span class="nav-number">1.3.0.2.</span> <span class="nav-text">maven 设置 setting</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#打包推送镜像"><span class="nav-number">1.4.</span> <span class="nav-text">打包推送镜像</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#执行package命令"><span class="nav-number">1.4.1.</span> <span class="nav-text">执行package命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#bulid镜像"><span class="nav-number">1.4.2.</span> <span class="nav-text">bulid镜像</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#push镜像"><span class="nav-number">1.4.3.</span> <span class="nav-text">push镜像</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#在docker服务器端查看镜像"><span class="nav-number">1.4.4.</span> <span class="nav-text">在docker服务器端查看镜像</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查看镜像仓库"><span class="nav-number">1.4.5.</span> <span class="nav-text">查看镜像仓库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#启动镜像"><span class="nav-number">1.4.6.</span> <span class="nav-text">启动镜像</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#访问项目"><span class="nav-number">1.4.7.</span> <span class="nav-text">访问项目</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#整合jenkins实现自动部署"><span class="nav-number">1.5.</span> <span class="nav-text">整合jenkins实现自动部署</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#自动化部署"><span class="nav-number">1.5.1.</span> <span class="nav-text">自动化部署</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#jenkins构建项目"><span class="nav-number">1.5.2.</span> <span class="nav-text">jenkins构建项目</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#结语"><span class="nav-number">1.6.</span> <span class="nav-text">结语</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2018 &mdash; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">ctrl</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">站点字数总计&#58;</span>
    
    <span title="站点字数总计">19.6k</span>

  <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
        <span class="post-meta-divider">|</span>
        <span class="post-meta-item-icon"><i class="fa fa-eye"></i></span>
        总访问量：<span id="busuanzi_value_site_pv"></span>次
        <span class="post-meta-divider">|</span>
        <span class="post-meta-item-icon"><i class="fa fa-user"></i></span>
        总访客：<span id="busuanzi_value_site_uv"></span>人
  
  

  
</div>




 <!--

<div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v</div>



-->

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  

  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="//cdn.jsdelivr.net/jquery/2.1.3/jquery.min.js"></script>
  

  
  
    <script type="text/javascript" src="//cdn.jsdelivr.net/fastclick/1.0.6/fastclick.min.js"></script>
  

  
  
    <script type="text/javascript" src="//cdn.jsdelivr.net/jquery.lazyload/1.9.3/jquery.lazyload.min.js"></script>
  

  
  
    <script type="text/javascript" src="//cdn.jsdelivr.net/velocity/1.2.3/velocity.min.js"></script>
  

  
  
    <script type="text/javascript" src="//cdn.jsdelivr.net/velocity/1.2.3/velocity.ui.min.js"></script>
  

  
  
    <script type="text/javascript" src="//cdn.jsdelivr.net/fancybox/2.1.5/jquery.fancybox.pack.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v="></script>

  <script type="text/javascript" src="/js/src/motion.js?v="></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v="></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v="></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v="></script>
<script type="text/javascript" src="/js/src/post-details.js?v="></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v="></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'x7eMnnknw8DLXiaQHKGDXYth-gzGzoHsz',
        appKey: 'Vj1vJImPLhxvGJIpsiDppAGm',
        placeholder: 'ヾﾉ≧∀≦)o来啊，快活啊!',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("x7eMnnknw8DLXiaQHKGDXYth-gzGzoHsz", "Vj1vJImPLhxvGJIpsiDppAGm");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  

  

  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/wanko.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":false},"log":false,"tagMode":false});</script></body>
</html>
